package controllers;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.reflect.*;

import entities.Template;

public class DemoServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String url = "http://localhost:8888/service/get-templates";
		
		try {
			InputStream input = new URL(url).openStream();
			Reader json = new InputStreamReader(input, "UTF-8");
			
			Type listType = new TypeToken<List<Template>>(){}.getType();
			List<Template> templates = new Gson().fromJson(json, listType);
			
			for (Template template: templates) {
				System.out.println(template.getId());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
